<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row v-show="showSearch">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
          <el-form-item label="标题" prop="title" >
            <el-input placeholder="请输入标题" v-model="state.queryForm.title" />
          </el-form-item>
          <el-form-item label="栏目" prop="columnId">
            <el-select v-model="state.queryForm.columnId" placeholder="请选择栏目">
              <el-option v-for="item in columnList" :key="item.columnId" :label="item.columnName" :value="item.columnId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="state">
            <el-select v-model="state.queryForm.state" placeholder="请选择状态">
              <el-option :label="item.label" :value="item.value" v-for="(item, index) in wz_state_type" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="search" type="primary" @click="getDataList">
              查询
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button type="primary" class="ml10" icon="folder-add" @click="formDialogRef.openDialog()">
            新增文章
          </el-button>

        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border
        :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
        @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column type="index" label="序号" width="60" />
          <el-table-column prop="title" label="标题"  show-overflow-tooltip/>
          <el-table-column prop="columnName" label="栏目"  show-overflow-tooltip/>
          <el-table-column prop="createBy" label="创建人"  show-overflow-tooltip/>
          <el-table-column prop="createTime" label="创建时间"  show-overflow-tooltip/>
        <el-table-column prop="state" label="状态"  show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="wz_state_type" :value="scope.row.state"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button
                :icon="scope.row.state == 0 ? 'Position' : 'Remove'" text type="primary"
                @click="handlePublish(scope.row)">
              {{scope.row.state == 0 ? '发布' : '撤回'}}
            </el-button>
            <el-button icon="edit-pen" text type="primary"
              @click="formDialogRef.openDialog(scope.row.articleId)"
            v-if="scope.row.state == 0">编辑</el-button>
            <el-button icon="delete" text type="primary"  @click="handleDelete([scope.row.articleId])"
                       v-if="scope.row.state == 0">删除</el-button>
            <el-button icon="View" text type="primary"
                       @click="previewDialogRef.previewDialog(scope.row.remark)">预览</el-button>

          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
<!--预览-->
    <preview-dialog ref="previewDialogRef"></preview-dialog>

  </div>
</template>

<script setup lang="ts" name="systemWzArticle">
import { BasicTableProps, useTable } from "/@/hooks/table";
import {fetchList, delObjs, releaseOrRevoke} from "/@/api/Article/wzArticle";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { useDict } from '/@/hooks/dict';
import DictTag from "/@/components/DictTag/index.vue";
import {getSonList} from "/@/api/wzjiang/wzColumn";



const PreviewDialog = defineAsyncComponent(() => import('./component/preview.vue'));
const previewDialogRef = ref()
// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典
const { wz_state_type } = useDict('wz_state_type')
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
	tableStyle
} = useTable(state)

//栏目列表
const columnList = ref();
const getColumnList = async () => {
  getSonList().then((res: any) => {
    columnList.value = res.data
  })
}
getColumnList();
// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/pig/wzArticle/export',Object.assign(state.queryForm, { ids: selectObjs }), 'wzArticle.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { articleId: string }[]) => {
  selectObjs.value = objs.map(({ articleId }) => articleId);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
//发布/撤回
const handlePublish = async (row: any) => {
  try {
    if(row.state == 0){
      await useMessageBox().confirm('确定发布吗？');
    }else{
      await useMessageBox().confirm('确定撤回吗？');
    }
  } catch {
    return;
  }
  try {
    await releaseOrRevoke(row);
    getDataList();
    useMessage().success('操作成功');
  }
  catch (err: any) {
    useMessage().error(err.msg);
  }
}


</script>
